using System.Collections.Generic;
using System.Threading.Tasks;
using Comteck.Dto.Sys;
using Comteck.Winforms.Data;
using DevExpress.XtraTreeList;

namespace Comteck.Winforms.Infrastructure {
  /// <summary>
  /// 
  /// </summary>
  public static partial class BindHelper {

    /// <summary>
    /// 绑定菜单树（首页）
    /// </summary>
    /// <param name="treeList"></param>
    /// <param name="dataSource"></param>
    public static void BindMainMenu(TreeList treeList, object dataSource = null) {
      treeList.BeginUpdate();
      treeList.ClearNodes();
      treeList.KeyFieldName = nameof(MenuAuthDto.Id);
      treeList.ParentFieldName = nameof(MenuAuthDto.Parent_Id);
      if (dataSource != null) {
        treeList.DataSource = dataSource;
      }
      treeList.EndUpdate();

      // 默认选择第一行
      if (treeList.Nodes.Count > 0) {
        treeList.CollapseAll();
        treeList.FocusedNode = treeList.Nodes[0];
        //menu.FocusedNode.Expanded = true;
      }
    }

    /// <summary>
    /// 菜单
    /// </summary>
    /// <param name="control"></param>
    /// <param name="editValue"></param>
    public static async Task BindMenuAuth(object control, object editValue = null, List<MenuAuthDto> datasource = null) {
      var options = new BindHelperOption() {
        DisplayMember = nameof(MenuAuthDto.Name),
        ValueMember = nameof(MenuAuthDto.Id),
        EditValue = editValue,
        BindColumnOptions = new List<BindColumnOption>() {
          new BindColumnOption() { Caption = "菜单编号", Width = 60, FieldName = nameof(MenuAuthDto.Id) },
          new BindColumnOption() { Caption = "菜单名称", Width = 150, FieldName = nameof(MenuAuthDto.Name) },
        }
      };

      if (datasource == null) {
        datasource = await ServerDbDataHelper.GetLastMenuListAsync();
      }

      Bind(control, options, datasource);
    }

  }
}
